package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.system.domain.Goods;
import com.ruoyi.system.mapper.GoodsMapper;
import com.ruoyi.system.service.BaseServiceImpl;
import com.ruoyi.system.service.GoodsService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;


@Service
public class GoodsServiceImpl extends BaseServiceImpl<GoodsMapper, Goods> implements GoodsService {


    @Override
    public Goods getByPrice(BigDecimal price) {

        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.lt("min_price", price);
        queryWrapper.ge("max_price", price);

        List<Goods> list = this.baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return this.getById(1);
        }


        int i = RandomUtil.randomInt(list.size());

        return list.get(i);

    }
}
